package com.inossem.warehousemanage_tv.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.flexbox.FlexboxLayout;
import com.inossem.warehousemanage_tv.R;
import com.inossem.warehousemanage_tv.bean.MatsBean;
import com.inossem.warehousemanage_tv.utils.Utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 仓库概览-物料列表-批次列表
 *
 * @author 詹建宇
 * @time on 2019/2/14 15:36
 * @email jianyu.zhan@inossem.com
 */
public class BatchAdapter extends BaseAdapter {

    private Context context;
    private List<MatsBean.MaterialListBean.BatchListBean> list;
    private Integer rid;
    private MatsBean.MaterialListBean matBean;

    public BatchAdapter(Context context, List<MatsBean.MaterialListBean.BatchListBean> list, MatsBean.MaterialListBean matBean) {
        this.context = context;
        this.list = list;
        this.matBean = matBean;
        rid = null;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.view_ware_batchs_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        MatsBean.MaterialListBean.BatchListBean bean = list.get(position);
        if (position % 2 == 0) {
            viewHolder.headLayout.setBackgroundResource(R.drawable.item_button_down_bg);
        } else {
            viewHolder.headLayout.setBackgroundResource(R.drawable.item_button_down);
        }

        viewHolder.batchTv.setText("批次:\t" + bean.getBatch_code());

        String spec_stock = bean.getSpec_stock();
        if (TextUtils.isEmpty(spec_stock)) {
            viewHolder.wbsCodeLayout.setVisibility(View.GONE);
            viewHolder.wbsNameLayout.setVisibility(View.GONE);
            Utils.setText(viewHolder.specialTv, "");
            Utils.setText(viewHolder.wbsCodeTv, "");
            Utils.setText(viewHolder.wbsNameTv, "");
        } else {
            viewHolder.wbsCodeLayout.setVisibility(View.VISIBLE);
            viewHolder.wbsNameLayout.setVisibility(View.VISIBLE);
            Utils.setText(viewHolder.specialTv, "特殊库存标识:\t" + spec_stock);
            Utils.setText(viewHolder.wbsCodeTv, bean.getWbs_code());
            Utils.setText(viewHolder.wbsNameTv, bean.getWbs_name());
        }
        Utils.setText(viewHolder.batchInventoryTv, "数量:\t" +bean.getBatch_number() + matBean.getName_zh());

        if (null != rid && position == rid) {
            viewHolder.flexBoxLayout.setVisibility(View.VISIBLE);
            viewHolder.iv_visibility.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_up_down));
            viewHolder.headLayout.setBackgroundResource(R.drawable.mat_item_not_ok);
        } else {
            viewHolder.flexBoxLayout.setVisibility(View.GONE);
            viewHolder.iv_visibility.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_up_pointer));
        }
        Utils.setText(viewHolder.inCodeTv, bean.getIn_code());
        Utils.setText(viewHolder.inPzTv, bean.getIn_voucher());
        Utils.setText(viewHolder.buyCodeTv, bean.getPurchase_code());
        Utils.setText(viewHolder.zjCodeTv, bean.getQuality_code());

        Date inDate = bean.getIn_date();
        if (null != inDate) {
            String time = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE).format(inDate);
            viewHolder.inTimeTv.setText(time);
        } else {
            Utils.setText(viewHolder.inTimeTv, "");
        }
        Date zjTimeDate = bean.getQuality_date();
        if (null != zjTimeDate) {
            String time = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE).format(zjTimeDate);
            viewHolder.zjTimeTv.setText(time);
        } else {
            Utils.setText(viewHolder.zjTimeTv, "");
        }

        viewHolder.headLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isGONEroVISIBLE(viewHolder, position);
            }
        });

        return convertView;
    }

    private void isGONEroVISIBLE(ViewHolder viewHolder, int position) {
        if (viewHolder.flexBoxLayout.getVisibility() == View.VISIBLE) {
            viewHolder.flexBoxLayout.setVisibility(View.GONE);
            viewHolder.iv_visibility.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_up_pointer));
            rid = null;
        } else {
            viewHolder.flexBoxLayout.setVisibility(View.VISIBLE);
            viewHolder.iv_visibility.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_up_down));
            rid = position;
        }
        notifyDataSetChanged();
    }

    private class ViewHolder {
        private LinearLayout headLayout;
        private TextView batchTv, specialTv, batchInventoryTv;
        private ImageView iv_visibility;
        private FlexboxLayout flexBoxLayout;
        private LinearLayout wbsCodeLayout, wbsNameLayout;
        private TextView inCodeTv, inPzTv, inTimeTv, wbsCodeTv, wbsNameTv, buyCodeTv, zjCodeTv, zjTimeTv;

        public ViewHolder(View view) {
            headLayout = view.findViewById(R.id.headLayout);
            batchTv = view.findViewById(R.id.batchTv);
            specialTv = view.findViewById(R.id.specialTv);
            batchInventoryTv = view.findViewById(R.id.batchInventoryTv);
            iv_visibility = view.findViewById(R.id.iv_visibility);
            flexBoxLayout = view.findViewById(R.id.flexBoxLayout);
            wbsCodeLayout = view.findViewById(R.id.wbsCodeLayout);
            wbsNameLayout = view.findViewById(R.id.wbsNameLayout);
            inCodeTv = view.findViewById(R.id.inCodeTv);
            inPzTv = view.findViewById(R.id.inPzTv);
            inTimeTv = view.findViewById(R.id.inTimeTv);
            wbsCodeTv = view.findViewById(R.id.wbsCodeTv);
            wbsNameTv = view.findViewById(R.id.wbsNameTv);
            buyCodeTv = view.findViewById(R.id.buyCodeTv);
            zjCodeTv = view.findViewById(R.id.zjCodeTv);
            zjTimeTv = view.findViewById(R.id.zjTimeTv);
        }
    }

}
